using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace F1RacingGame
{
    public class LeaderboardEntry
    {
        public string PlayerName { get; set; }
        public float Time { get; set; }
        public string Car { get; set; }
        public string Track { get; set; }
        public DateTime Date { get; set; }
        
        public LeaderboardEntry(string playerName, float time, string car, string track)
        {
            PlayerName = playerName;
            Time = time;
            Car = car;
            Track = track;
            Date = DateTime.Now;
        }
        
        public override string ToString()
        {
            return $"{PlayerName} - {Time:F2}s - {Car} - {Track} - {Date:yyyy-MM-dd}";
        }
    }
    
    public class Leaderboard
    {
        private static Leaderboard instance;
        private List<LeaderboardEntry> entries;
        private const string LEADERBOARD_FILE = "leaderboard.txt";
        private const int MAX_ENTRIES = 10;
        
        private Leaderboard()
        {
            entries = new List<LeaderboardEntry>();
            LoadLeaderboard();
        }
        
        public static Leaderboard Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Leaderboard();
                }
                return instance;
            }
        }
        
        public void AddEntry(string playerName, float time, string car, string track)
        {
            var entry = new LeaderboardEntry(playerName, time, car, track);
            entries.Add(entry);
            
            // 按时间排序（时间越短越好）
            entries = entries.OrderBy(e => e.Time).ToList();
            
            // 只保留前10名
            if (entries.Count > MAX_ENTRIES)
            {
                entries = entries.Take(MAX_ENTRIES).ToList();
            }
            
            SaveLeaderboard();
        }
        
        public List<LeaderboardEntry> GetTopEntries(int count = 10)
        {
            return entries.Take(count).ToList();
        }
        
        public bool IsNewRecord(float time)
        {
            if (entries.Count < MAX_ENTRIES)
                return true;
                
            return time < entries.Max(e => e.Time);
        }
        
        private void LoadLeaderboard()
        {
            try
            {
                if (File.Exists(LEADERBOARD_FILE))
                {
                    string[] lines = File.ReadAllLines(LEADERBOARD_FILE);
                    entries.Clear();
                    
                    foreach (string line in lines)
                    {
                        if (!string.IsNullOrEmpty(line))
                        {
                            var parts = line.Split('|');
                            if (parts.Length >= 5)
                            {
                                var entry = new LeaderboardEntry(
                                    parts[0], // PlayerName
                                    float.Parse(parts[1]), // Time
                                    parts[2], // Car
                                    parts[3]  // Track
                                );
                                entry.Date = DateTime.Parse(parts[4]); // Date
                                entries.Add(entry);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"加载排行榜失败: {ex.Message}");
            }
        }
        
        private void SaveLeaderboard()
        {
            try
            {
                var lines = entries.Select(e => 
                    $"{e.PlayerName}|{e.Time}|{e.Car}|{e.Track}|{e.Date:yyyy-MM-dd HH:mm:ss}");
                
                File.WriteAllLines(LEADERBOARD_FILE, lines);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存排行榜失败: {ex.Message}");
            }
        }
        
        public void ClearLeaderboard()
        {
            entries.Clear();
            try
            {
                if (File.Exists(LEADERBOARD_FILE))
                {
                    File.Delete(LEADERBOARD_FILE);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"清除排行榜失败: {ex.Message}");
            }
        }
    }
} 